/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 * */

// AmzServer - Tototoy's server model.
// Author: tonyjobmails@gmail.com (tonyhack).
//
// Define class UDPSocket.
//
// UDPSocket is a wrapper of socket and it only supports UDP connection.
//

#ifndef __UDP__SOCKET__H
#define __UDP__SOCKET__H

#include <sys/socket.h>
#include <string>

#include "noncopyable.h"
#include "common.h"

namespace amz {

class UDPSocket : public noncopyable {
 public:
  explicit UDPSocket(int socket = 0);
  virtual ~UDPSocket();

  // Create a socket.
  bool CreateSocket();

  // Bind IP and port.
  bool Bind(const char *ip, uint16 port);

  // Close the socket.
  bool Close();

  // Send/Receive datas.
  // return number of bytes if read/receive success.
  // return -1 is an error.
  int Send(const char *message, int size, const struct sockaddr *to, int addr_size = sizeof(struct sockaddr), int flag = 0);
  int Receive(char *message, int size, struct sockaddr *from, int addr_size = sizeof(struct sockaddr), int flag = 0);

  bool SetNonblock();

 private:
  int socket_;
};

}  // namespace amz

#endif  // __UDP__SOCKET__H

